/**
 * Created by 瑱彬 on 2015/6/2.
 */
var switchEditor = function (e) {
    if ($(e).html() == "编辑") {
        $(e).html("收起");
        $(e).css("background-image", "url('" + CONTEXT + "/images/sharp6.png')");
    } else {
        $(e).html("编辑");
        $(e).css("background-image", "url('" + CONTEXT + "/images/sharp5.png')");
    }

}
var uploadCallback = function (obj) {
    if (obj.type == "avatarSuccess") {
        $.get(CONTEXT+"/user/modifyAvatar?imgUrl="+obj.data.msg,function(data){
            if(data.ret == SUCCESS){
                showTopBox("上传成功", function () {
                    window.location.reload();
                });
            }else{
                showTopBox(data.msg,function(){
                    window.location.reload();
                });
            }
        });
    }else if(obj.type == "cancel"){
        $(".uploadDiv").hide();
    }
    console.log(obj);
}

var modifyInfo = function(){
    var name = $("input[name='name']").val();
    var alias = $("input[name='alias']").val();
    var realname = $("input[name='realname']").val();
    var role = $("input[name='role']").val();
    var email = $("input[name='email']").val();
    var number = $("input[name='number']").val();
    var summary = $("#summary").val();

    $.post(CONTEXT + '/user/modifyInfo',{
        name : name,
        alias : alias,
        realname :realname,
        role :role,
        email:email,
        number:number,
        summary:summary
    }, function(data) {
        if(data.ret == SUCCESS){
            showTopBox("更新成功", function () {
                window.location.reload();
            });
        }else{
            showTopBox(data.msg);
        }
    });
}

var modifyPassword = function(){
    var newPassword = $("input[name='newPassword']").val();
    var oldPassword = $("input[name='oldPassword']").val();
    var confirmPassword = $("input[name='confirmPassword']").val();
    if(newPassword != confirmPassword){
        showTopBox("密码不统一");
    }else if(newPassword.length < 6){
        showTopBox("密码不能小于6位数");
    }
    $.post(CONTEXT+"/user/modifyPassword",{
        newPassword:newPassword,oldPassword:oldPassword
    },function(data){
        if(data.ret == SUCCESS){
            showTopBox("更新成功", function () {
                window.location.reload();
            });
        }else{
            showTopBox(data.msg);
        }
    });



}

$(document).ready(function () {
    var personalInfo = $("#personalInfo").html();
    var avatarImg = $("#avatarImg").html();
    var passwordInfo = $("#passwordInfo").html();

    $("#personalSubmit").on("click",function(){
        modifyInfo();
    });

    $("#passwordSubmit").on("click",function(){
        modifyPassword();
    });

    $("#personalEditor").on("click", function () {
        if ($(this).html() == "编辑") {
            $("#personalFormContainer").slideDown();
            $("#personalInfo").html("");

        } else {
            $("#personalFormContainer").slideUp();
            $("#personalInfo").html(personalInfo);
        }
        switchEditor(this);
    });

    $("#avatarEditor").on("click", function () {
        if ($(this).html() == "编辑") {
            $("#avatarFormContainer").slideDown();
            $("#avatarImg").html("");

        } else {
            $("#avatarFormContainer").slideUp();
            $("#avatarImg").html(avatarImg);
        }
        switchEditor(this);
    });

    $("#passwordEditor").on("click", function () {
        if ($(this).html() == "编辑") {
            $("#passwordFormContainer").slideDown();
            $("#passwordInfo").html("");

        } else {
            $("#passwordFormContainer").slideUp();
            $("#passwordInfo").html(passwordInfo);
        }
        switchEditor(this);
    })
    var W = $(window).width();

    var flashvars = {
        js_handler: "uploadCallback",
        swfID: "avatarEdit",
        picSize: "5242880",
        sourceAvatar: "http://ww1.sinaimg.cn/large/64a97dc7jw1e1xlbtnhhhj.jpg",
        avatarLabel: "头像预览，请注意清晰度",
        avatarAPI: CONTEXT + "/avatar/upload",
        avatarSize: "120,120",
        avatarSizeLabel: "120*120"
    };
    var params = {
        menu: "false",
        scale: "noScale",
        allowFullscreen: "true",
        allowScriptAccess: "always",
        bgcolor: "",
        wmode: "transparent" // can cause issues with FP settings & webcam
    };
    var attributes = {
        id: "AvatarUpload"
    };

    var initSwf = 0;

    $(".uploadImgA").on("click", function () {
        $(".uploadDiv").css("left", (W - 750) / 2);
        $(".uploadDiv").show("fast", function () {
            if(initSwf == 0){
                swfobject.embedSWF(
                    CONTEXT + "/thirdparts/avatar/avatarUpload.swf",
                    "altContent", "100%", "100%", "10.0.0",
                    CONTEXT + "/thirdparts/avatar/expressInstall.swf",
                    flashvars, params, attributes);
                initSwf = 1;
            }
        });
    })

});